package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单参数校验
 */
@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {


    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        // 向下转型
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        Long userId = createOrderContext.getUserId();
        Long addressId = createOrderContext.getAddressId();
        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        BigDecimal orderTotal = createOrderContext.getOrderTotal();
        String streetName = createOrderContext.getStreetName();
        String tel = createOrderContext.getTel();
        String userName = createOrderContext.getUserName();

        if (userId == null || addressId == null || cartProductDtoList == null || orderTotal == null || (streetName == null || "".equals(streetName))|| (tel == null || "".equals(tel))|| (userName == null || "".equals(userName))) {
            throw new BizException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(), OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }

        return true;
    }
}
